﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

namespace GeniusAdminWebApplication
{
    public partial class category : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IList<Category> cl = getAllCategory();
            foreach (Category c in cl)
            {
                Panel p_item = new Panel();
                p_item.CssClass = "category_item";
                p_item.Controls.Add(new LiteralControl("<span class='categoryOrder'>" + c.categoryOrder + "</span>"));
                p_item.Controls.Add(new LiteralControl("<span class='categoryName'>" + c.categoryName + "</span>"));
                p_item.Controls.Add(new LiteralControl("<span class='chineseName'>" + c.chineseName + "</span>"));

                p_item.Controls.Add(new LiteralControl("<span class='catAction'><input type='button' value='修改名称' onclick='modifyCategoryChineseName(" + c.id + ",\"" + c.chineseName + "\");' /></span>"));
                p_item.Controls.Add(new LiteralControl("<span class='catAction'><input type='button' value='调整顺序' onclick='modifyCategoryOrder(" + c.id + "," + c.categoryOrder + ");' /></span>"));
                ph_category.Controls.Add(p_item);
            }
        }

        //所有用户
        private IList<Category> getAllCategory()
        {
            CategoryService cs = new CategoryService();
            return cs.getCategoryList();
        }

        [WebMethod]
        public static string ajaxAddCategory(int categoryOrder, string categoryName, string chineseName)
        {
            CategoryService cs = new CategoryService();
            Hashtable ht = cs.addCategory(categoryOrder, categoryName, chineseName);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

        [WebMethod]
        public static string modifyCategoryChineseName(int id, string newName)
        {
            CategoryService cs = new CategoryService();
            Category category = cs.getCategoryById(id);
            Hashtable ht = cs.changeCategory(category.id, category.categoryName, newName);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

        [WebMethod]
        public static string modifyCategoryOrder(int id, int newOrder)
        {
            CategoryService cs = new CategoryService();
            Hashtable ht = cs.changeOrder(id, newOrder);
            string result = (string)ht["result"];
            string message = (string)ht["message"];
            return "{" +
                        "result:'" + result + "'," +
                        "message:'" + message + "'" +
                    "}";
        }

    }
}